package cn.erperp.cdc.maxwell.msg;

import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.common.ClientException;
import com.aliyun.mns.common.ServiceException;
import com.aliyun.mns.model.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class MessageSenderMns implements MessageSenderInterface {

    @Autowired
    private MNSClient mnsClient;

    @Override
    public String send(String queueName, String messageBody,MQConfig mqConfig){
        Message messageReturn = null;
        try {
            CloudQueue queue = mnsClient.getQueueRef(queueName);
            Message message = new Message();
            message.setMessageBody(messageBody);
            messageReturn = queue.putMessage(message);
        } catch (ServiceException e) {
            log.error(e.getErrorCode(),e);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return messageReturn.getMessageId();
    }
}
